﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using BusinessRule.Common;
using System.Configuration;

namespace OracleTableSync
{
    public partial class OracleSyncTable : ServiceBase
    {
        CaptureAndPush cp = new CaptureAndPush();
        ReceiveAndDeal rd = new ReceiveAndDeal();
        SyncTableAllData sa = new SyncTableAllData();

        //定时器
        private System.Timers.Timer m_tCaptureAndPush;//捕捉和推送--变更记录

        private System.Timers.Timer m_tReceiveAndDeal;//接收处理--变更记录

        private System.Timers.Timer m_tTableAllData;//表全量同步定时器


        public OracleSyncTable()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        { 
            LogHelp.SetConfig();
            LogHelp.WriteLog("服务启动");
            int intSync = Convert.ToInt32(ConfigurationManager.AppSettings["sync"].ToString());

            SyncTimerStatr(ref m_tCaptureAndPush, m_tCaptureAndPush_Elapsed, intSync * 5);
            SyncTimerStatr(ref m_tReceiveAndDeal, m_tReceiveAndDeal_Elapsed, intSync * 5);
            SyncTimerStatr(ref m_tTableAllData, m_tTableAllData_Elapsed, intSync * 5);
        }

        /// <summary>
        /// 传参启动新线程并运行方法
        /// </summary>
        /// <param name="timer"></param>
        /// <param name="m_timerElapsed"></param>
        private void SyncTimerStatr(ref System.Timers.Timer timer, Action<object, System.Timers.ElapsedEventArgs> m_timerElapsed, int intSync)
        {
            timer = new System.Timers.Timer();
            timer.Enabled = false;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timerElapsed);

            timer.Interval = intSync * 1000;
            timer.Enabled = true;
            timer.Start();
        }

        //捕捉和推送--变更记录
        private void m_tCaptureAndPush_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_tCaptureAndPush.Enabled = false;
                cp.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("捕捉和推送", myError);
            }
            finally
            {
                m_tCaptureAndPush.Enabled = true;
            }
        }

        //接收处理--变更记录
        private void m_tReceiveAndDeal_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_tReceiveAndDeal.Enabled = false;
                rd.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("捕捉和推送", myError);
            }
            finally
            {
                m_tReceiveAndDeal.Enabled = true;
            }
        }

        //表全量同步定时器
        private void m_tTableAllData_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_tTableAllData.Enabled = false;
                sa.Excute();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("表全量同步定时器", myError);
            }
            finally
            {
                m_tTableAllData.Enabled = true;
            }
        }


        protected override void OnStop()
        {
        }
    }
}
